var HOME='home';
var QUERY1='q1';
var QUERY2='q2';
var QUERY3='q3';
var QUERY4='q4';

//function to initialize the page
var init = function() {
	//showing the home tab on initializing
	showTab(QUERY1);
	//adding event listeners to the tabs
	$('#tabs a').click(function(event) {
		showTab(event.currentTarget.id);
	});
}

//function to show the tab
var showTab = function(entity) {
	//remove the active class from all the tabs
	$('.tab').removeClass("active");
	//setting the active class to the selected tab
	$('#'+entity).addClass("active");
	//hiding all the tabs
	$('.g-unit').hide();
	//showing the selected tab
	$('#' + entity + '-tab').show();
	//hiding the message block
	$('.message').hide();
	//hiding the create block
	showHideCreate(entity, false);
	if(entity!=HOME)
		$('#'+entity+'-search-reset').click();
}

//function to show/hide create block for an entity in a tab 
var showHideCreate = function(entity, show) {
	//checking if the block is show or not
	if (show) {
		//hiding the search container
		$('#' + entity + '-search-ctr').hide();
		//hiding the list container
		$('#' + entity + '-list-ctr').hide();
		//showing the create container
		$('#' + entity + '-create-ctr').show();
		
	} else {
		//showing the search container
		$('#' + entity + '-search-ctr').show();
		//showing the list container
		$('#' + entity + '-list-ctr').show();
		//hiding the create container
		$('#' + entity + '-create-ctr').hide();
		//checking if the entity is not a home then populating the list of the entity
		if(entity!=HOME)
			populateList(entity,null);
	}
}

// function to get the data by setting url, filter, success function and error function
var getData=function(url,filterData,successFn,errorFn){
	// making the ajax call
	$.ajax({
		url : url,
		type : "GET",
		data:filterData,
		success : function(resp) {
			//calling the user defined success function
			if(successFn)
			successFn(resp);	
		},
	error:function(e){
		//calling the user defined error function
		if(errorFn)
		 errorFn(e);
	}
	});
}

//function to populate the list of an entity
var populateList=function(entity, filter){
	//specifying the success function. When the ajax response is successful then the following function will be called

	var successFn=function(resp){
		var data='';
		if(resp){
			//getting the data from the response object
			data=resp.data;
		}
		//creating the html content
		var htm='';
		if(data.length > 0){
			for (var i=0;i<data.length;i++){
				//creating a row
				htm+='<tr>';
				switch(entity)
				{
				case QUERY1:
				case QUERY3:
					htm+= '<td>'+data[i].name+'</td><td>'+data[i].version+'</td><td>'+data[i].mobile+'</td><td>'+data[i].count+'</td><td>'+data[i].percentage+'</td>';
					break;
				case QUERY2:
					htm+= '<td>'+data[i].name+'</td><td>'+data[i].count+'</td><td>'+data[i].percentage+'</td>';
					break;
				case QUERY4:
					htm+= '<td>'+data[i].sis_nome+'</td><td>'+data[i].sis_versao+'</td><td>'+data[i].sis_movel+'</td><td>'+data[i].nav_nome+'</td><td>'+data[i].nav_versao+'</td><td>'+data[i].count+'</td><td>'+data[i].percentage+'</td>';
					break;
				default:
					htm+=""; 
				}
			}
		}
		else{
			//condition to show message when data is not available
			var thElesLength=$('#'+entity+'-list-ctr table thead th').length;
			htm+='<tr><td colspan="'+thElesLength+'">No items found</td></tr>';
		}
		$('#'+entity+'-list-tbody').html(htm);
	}
	getData("/userstory10?q="+entity,filter,successFn,null);
}